#include <bits/stdc++.h>
using namespace std;
const int N = 10000 + 10;
int n, a[N], f[N], res = -1;
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 0; i < n; ++i) {
    f[i] = a[i];
    for (int j = 0; j < i; ++j) {
      if (a[j] < a[i]) {
        f[i] = max(f[i], f[j] + a[i]);
      }
    }
    res = max(res, f[i]);
  }
  cout << res << endl;
}
